package com.example.testtransition

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Parcelable
import android.transition.*
import android.util.Log
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import com.bumptech.glide.Glide
import com.example.testtransition.databinding.ActivityMomentDetailBinding
import com.example.ycshareelement.YcShareElement
import com.example.ycshareelement.transition.*
import java.util.concurrent.atomic.AtomicBoolean

class MomentDetailActivity : AppCompatActivity() {

    private fun initWindowTheme() {
        if (Build.VERSION.SDK_INT >= 21) {
            val option = (View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
            window?.navigationBarColor = ContextCompat.getColor(this, R.color.mi_bg_light_gray_color)
            window?.decorView?.systemUiVisibility = option
            window?.statusBarColor = Color.TRANSPARENT
        }
        window?.decorView?.post {
            //监听是否有虚拟键盘
            val bottomBarHeight = ScreenUtil.getNavigationBarHeight(this)
            if (bottomBarHeight > 0 && ScreenUtil.isNavigationBarExist(this)) {
                val params = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT)
                params.bottomMargin = bottomBarHeight
                binding?.mainLayout?.layoutParams = params
            }
        }
    }

    var binding : ActivityMomentDetailBinding? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (Build.VERSION.SDK_INT >= 28) {
            window?.attributes?.apply {
                layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
            }
        }
        window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)

        val transitionSet = TransitionSet()
        transitionSet.addTransition(ChangeBounds())
        transitionSet.addTransition(ChangeClipBounds())
        transitionSet.addTransition(ChangeImageTransform())
        transitionSet.addTransition(ChangeTextTransition())
//        transitionSet.addTransition(ChangeOnlineImageTransition())

        window.sharedElementEnterTransition = transitionSet
            .apply{
            addTarget(R.id.nickname)
            addTarget(R.id.appiv)
            duration = 2000L
            Log.d("MomentDetailActivity", "sharedElementEnterTransition  set Target")
        }

        window.sharedElementExitTransition = transitionSet
            .apply{
            addTarget(R.id.nickname)
            addTarget(R.id.appiv)
            duration = 250L
            Log.d("MomentDetailActivity", "sharedElementExitTransition  set Target")
        }

        // setContentView(R.layout.activity_moment_detail)
        binding = DataBindingUtil.setContentView<ActivityMomentDetailBinding>(this, R.layout.activity_moment_detail)

        initWindowTheme()

        val album = intent.getParcelableExtra<MyAdapter.Album>("album")
        Log.d("MomentDetailActivity", "album :: $album")

        binding?.nickname?.text = album?.title
        binding?.txtUID?.text = "ID: ${album?.artist}"

        binding?.nickname?.transitionName = "NICKNAME"
        binding?.appiv?.transitionName = "AVATAR"

        val elens =
        YcShareElement.setEnterTransitions(this, object : IShareElements {
            override fun getShareElements(): Array<ShareElementInfo<Parcelable>> {
                return arrayOf(
                    ShareElementInfo<Parcelable>(binding!!.appiv!!, CornerImageViewStateSaver()),
                    ShareElementInfo<Parcelable>(binding!!.nickname!!, TextViewStateSaver())
                )
            }
        }, false)

        val url =  "https://img.520yidui.com/uploads/member_avatar/avatar/98068/698f78863aae2eeff044d63d68d7dbaf.jpg@!normal"

        binding?.appiv?.let { it ->
            Glide.with(this)
                .load(url)
                .into(it)
        }
    }

}